package org.lanqiao.tjut.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.TBStudentBean;
import org.lanqiao.tjut.model.ModelFactory;
import org.lanqiao.tjut.myUtils.MyUtils;

/**
 * 学生信息添加 StudentInfoAdd
 */
@WebServlet("/StudentInfoAddServlet")
@MultipartConfig(maxFileSize = 5000000) // 注意设置文件大小的单位是 bit
public class StudentInfoAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 调用换行方法，将request对象中的参数依次赋值给实体类
		TBStudentBean stuB = MyUtils.convertRequest2Bean(request, TBStudentBean.class, "uploadfiles/stuImgs/",
				"stuImg");
		// 输出打印前台表单数据
		System.out.println("stu::" + stuB.toString());

		// 调用model层进行数据库保存
		int i = ModelFactory.getStudentInfoModelInstance().saveOrUpdateStudentInfo(stuB);
		// 保存之后的跳转处理
		if (i > 0) {
			// 以下两种跳转方式 二选一
			// 保存成功--跳转到查询页面进行结果显示
			 request.getRequestDispatcher("WEB-INF/logined/studentInfoManager/studentInfoAjaxQueryPager.jsp").forward(request,
			 response);
			// 保存成功--跳转到保存结果显示页面进行结果显示
			/*request.getRequestDispatcher("WEB-INF/logined/studentInfoManager/studentInfoAddDetail.jsp").forward(request,
			  response);*/

			
		} else {
			// 保存失败
			response.getWriter().write(
					"<script language='javascript'>alert('数据保存或修改失败!');"
					+ "location.href='DispatchCommServlet?"
					+ "jp=WEB-INF/logined/studentInfoManager/studentInfoAdd';</script>");
		}
	}

}
